//
//  CGFavoritesViewController.m
//  SchoolClient
//
//  Created by mac on 15/11/3.
//  Copyright © 2015年 mac. All rights reserved.
/*
 *collection/delete.app userId=用户id&schoolId=学校ID
 *companyId=企业ID  2种数据公用一个方法  根据类型改变参数即可
 */


#import "CGFavoritesViewController.h"
#import "CGCollectionCompanyFrame.h"
#import "CGCollectionSchoolFrame.h"

@interface CGFavoritesViewController ()

@property(nonatomic,strong)UIView *bottomView;
@property(nonatomic,strong)UIView *firstLine;
@property(nonatomic,strong)UIView *secondLine;
@property(nonatomic,strong)UIButton *releaseButton;           //发布中
@property(nonatomic,strong)UIButton *overdueButton;           //已过期
@property(nonatomic,strong)UIButton *selectAllButton;         //全部选择
@property(nonatomic,strong)UIButton *deleteButton;            //删除
@property(nonatomic,strong)NSMutableArray *releaseProducts;   //发布中产品
@property(nonatomic,strong)NSMutableArray *overdueProducts;   //已过期产品
@property(nonatomic,strong)NSString *model;

@property(nonatomic,strong)UITableView *releaseTableView;     //发布中表格内容
@property(nonatomic,strong)UITableView *overdueTableView;     //已过期表格内容

@property(nonatomic,strong)CGInfoCompany *company;
@property(nonatomic,strong)CGInfoSchool *school;

@end

@implementation CGFavoritesViewController

-(NSMutableArray *)releaseProducts{
    if (!_releaseProducts) {
        _releaseProducts = [NSMutableArray array];
    }
    return _releaseProducts;
}

-(NSMutableArray *)overdueProducts{
    if (!_overdueProducts) {
        _overdueProducts = [NSMutableArray array];
    }
    return _overdueProducts;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [Tools getColor:@"ebebeb"];
    self.title = @"信息收藏";
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc]initWithTitle:@"编辑" style:UIBarButtonItemStylePlain target:self action:@selector(enterEditing:)];
    
    [self initCategoryView];
    [self initReleaseTableView];
    [self initOverdueTableView];
    
    [self initBottomView];
    
    //默认显示社会招聘信息
    self.model = @"0";
    [self fetchCollectionProducts];
    
    //上拉加载旧数据
    [self.releaseTableView addFooterWithTarget:self action:@selector(loadMoreStatus)];
    
    
    //使cell的线条充满
    if ([self.releaseTableView respondsToSelector:@selector(setSeparatorInset:)] || [self.overdueTableView respondsToSelector:@selector(setSeparatorInset:)]) {
        
        [self.releaseTableView setSeparatorInset:UIEdgeInsetsZero];
        [self.overdueTableView setSeparatorInset:UIEdgeInsetsZero];
    }
    if ([self.releaseTableView respondsToSelector:@selector(setLayoutMargins:)] || [self.overdueTableView respondsToSelector:@selector(setLayoutMargins:)]) {
        
        [self.releaseTableView setLayoutMargins:UIEdgeInsetsZero];
        [self.overdueTableView setLayoutMargins:UIEdgeInsetsZero];
        
    }
    
}

#pragma mark 添加分类招聘按钮
-(void)initCategoryView{
    
    UIView *titleView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 49)];
    titleView.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:titleView];
    //发布中Button
    CGRect aFrame = CGRectMake(0, 0, SCREEN_WIDTH/2, 49);
    CGRect bFrame = CGRectMake(0, 49, SCREEN_WIDTH/2, 2);
    self.releaseButton = [[UIButton alloc]initWithFrame:aFrame];
    [self.releaseButton setTitle:@"发布中" forState:UIControlStateNormal];
    [self.releaseButton setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    [self.releaseButton setTitleColor:[UIColor orangeColor] forState:UIControlStateSelected];
    self.releaseButton.selected = YES;
    [self.view addSubview:self.releaseButton];
    self.firstLine = [[UIView alloc]initWithFrame:bFrame];
    self.firstLine.backgroundColor = [UIColor orangeColor];
    [self.view addSubview:self.firstLine];
    //添加点击事件
    [self.releaseButton addTarget:self action:@selector(collectionButtonClick:) forControlEvents:UIControlEventTouchUpInside];
    
    //已过期Button
    aFrame.origin.x += SCREEN_WIDTH/2;
    bFrame.origin.x += SCREEN_WIDTH/2;
    self.overdueButton = [[UIButton alloc]initWithFrame:aFrame];
    [self.overdueButton setTitle:@"已过期" forState:UIControlStateNormal];
    [self.overdueButton setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    [self.overdueButton setTitleColor:[UIColor orangeColor] forState:UIControlStateSelected];
    [self.view addSubview:self.overdueButton];
    self.secondLine = [[UIView alloc]initWithFrame:bFrame];
    self.secondLine.backgroundColor = [UIColor orangeColor];
    self.secondLine.hidden = YES;
    [self.view addSubview:self.secondLine];
    //添加点击事件
    [self.overdueButton addTarget:self action:@selector(collectionButtonClick:) forControlEvents:UIControlEventTouchUpInside];
}

-(void)initBottomView{
    
    //底部View
    CGRect bRect = CGRectMake(0, SCREEN_HEIGHT - 64, SCREEN_WIDTH, 49);
    UIView *bottomView = [[UIView alloc]initWithFrame:bRect];
    bottomView.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:bottomView];
    //全选Button
    CGRect cFrame = CGRectMake(0, 0, 90, 49);
    self.selectAllButton = [[UIButton alloc]initWithFrame:cFrame];
    [self.selectAllButton setImage:[UIImage imageNamed:@"mine_not_check_icon"] forState:UIControlStateNormal];
    [self.selectAllButton setImage:[UIImage imageNamed:@"mine_checked_icon"] forState:UIControlStateSelected];
    [self.selectAllButton setTitle:@"全选" forState:UIControlStateNormal];
    [self.selectAllButton setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    self.selectAllButton.imageEdgeInsets = UIEdgeInsetsMake(0, -15, 0, 0);
    //self.selectAllButton.titleEdgeInsets = UIEdgeInsetsMake(0, -10, 0, 0);
    [bottomView addSubview:self.selectAllButton];
    //添加按钮点击事件
    [self.selectAllButton addTarget:self action:@selector(selectAllButtonClick:) forControlEvents:UIControlEventTouchUpInside];
    
    //删除Button
    cFrame = CGRectMake(SCREEN_WIDTH - 170, 3, 160, 43);
    self.deleteButton = [[UIButton alloc]initWithFrame:cFrame];
    [self.deleteButton setBackgroundImage:[UIImage imageNamed:@"mine_delete_bg"] forState:UIControlStateNormal];
    [self.deleteButton setTitle:@"删除" forState:UIControlStateNormal];
    [self.deleteButton setTitleColor:[UIColor redColor] forState:UIControlStateNormal];
    //添加按钮点击事件
    [self.deleteButton addTarget:self action:@selector(deleteButtonClick) forControlEvents:UIControlEventTouchUpInside];
    [bottomView addSubview:self.deleteButton];
    
    self.bottomView = bottomView;
}



#pragma mark 添加表格
-(void)initTableView{
    
    CGRect aRect = CGRectMake(0,51,SCREEN_WIDTH,SCREEN_HEIGHT-64-51);
    self.tableView = [[UITableView alloc] initWithFrame:aRect  style:UITableViewStylePlain];
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleSingleLine;
    self.tableView.autoresizingMask = UIViewAutoresizingFlexibleWidth;
    self.tableView.backgroundColor = [UIColor clearColor];
    [self.view addSubview: self.tableView];
}

#pragma mark 添加发布中表格
-(void)initReleaseTableView{
    
    CGRect aRect = CGRectMake(0,51,SCREEN_WIDTH,SCREEN_HEIGHT-64-51);
    self.releaseTableView = [[UITableView alloc] initWithFrame:aRect  style:UITableViewStylePlain];
    self.releaseTableView.delegate = self;
    self.releaseTableView.dataSource = self;
    self.releaseTableView.separatorStyle = UITableViewCellSeparatorStyleSingleLine;
    self.releaseTableView.autoresizingMask = UIViewAutoresizingFlexibleWidth;
    self.releaseTableView.backgroundColor = [UIColor clearColor];
    [self.view addSubview: self.releaseTableView];
}

#pragma mark 添加已过期表格
-(void)initOverdueTableView{
    
    CGRect aRect = CGRectMake(0,51,SCREEN_WIDTH,SCREEN_HEIGHT-64-51);
    self.overdueTableView = [[UITableView alloc] initWithFrame:aRect  style:UITableViewStylePlain];
    self.overdueTableView.delegate = self;
    self.overdueTableView.dataSource = self;
    self.overdueTableView.separatorStyle = UITableViewCellSeparatorStyleSingleLine;
    self.overdueTableView.autoresizingMask = UIViewAutoresizingFlexibleWidth;
    self.overdueTableView.backgroundColor = [UIColor clearColor];
    self.overdueTableView.hidden = YES;
    [self.view addSubview: self.overdueTableView];
}




#pragma mark 发送请求获取产品
-(void)fetchCollectionProducts{
    
    NSMutableDictionary *parameters = [NSMutableDictionary dictionary];
    parameters[@"model"] = self.model;
    parameters[@"pageSize"] = [NSString stringWithFormat:@"%d",(int)SCREEN_HEIGHT/100];
    parameters[@"page"] = @"0";
    parameters[@"id"] = [CGAccountDefault shareAccount].user_id;
    
    ZYHRequest *request = [ZYHRequest new];
 
    [request postAsyncEncryptRequest:@"/collection/getData.app" parameters:parameters success:^(id responseData) {
        
        NSLog(@"信息收藏请求成功!!");
        //把接收到的数组对象变成model
        NSDictionary *jsonDic =  [NSJSONSerialization JSONObjectWithData:responseData options:NSJSONReadingMutableContainers error:NULL];
        NSString *value = jsonDic[@"data"];
        NSLog(@"result:%@",value);
        NSMutableArray *newProducts = [NSMutableArray array];
        if (value) {
            NSData *valueData = [value dataUsingEncoding:NSUTF8StringEncoding];
            newProducts = (NSMutableArray *)[CGInfo objectArrayWithJSONData:valueData];
        }
        if ([self.model isEqualToString:@"0"]) {
            self.releaseProducts = newProducts;
        }else{
            self.overdueProducts = newProducts;
        }
        
        [self.releaseTableView reloadData];
        [self.overdueTableView reloadData];
        
    } failure:^(NSError *error) {
        
        NSLog(@"信息收藏请求失败:%@",error);
    }];
    
}


#pragma mark 上拉加载
-(void)loadMoreStatus{
    
    NSMutableDictionary *parameters = [NSMutableDictionary dictionary];
    parameters[@"model"] = self.model;
    parameters[@"pageSize"] = [NSString stringWithFormat:@"%d",(int)SCREEN_HEIGHT/100];
    parameters[@"id"] = [CGAccountDefault shareAccount].user_id;
    
    if ([self.model isEqualToString:@"0"]) {
        if (!self.releaseProducts.count) {
            // 结束上拉加载
            [self.tableView footerEndRefreshing];
            return;                   //有数据才需要加载
        }else{
            parameters[@"page"] = [NSString stringWithFormat:@"%lu",(unsigned long)self.releaseProducts.count];
        }
        
    }else{
        if (!self.overdueProducts.count) {
            return;                     //有数据才需要加载
        }else{
            parameters[@"page"] = [NSString stringWithFormat:@"%lu",(unsigned long)self.overdueProducts.count];
        }
    }
    
    //检查网络是否可用
    if (![ZYHRequest NetWorkIsOK]) {
        [Tools alertWithTitle:@"温馨提示" andMessage:@"网络不可用"];
        // 结束上拉加载
        [self.tableView footerEndRefreshing];
        return;
    }
    
    ZYHRequest *request = [ZYHRequest new];
    [request postAsyncEncryptRequest:@"/collection/getData.app" parameters:parameters success:^(id responseData) {
        
        NSLog(@"过期信息加载请求成功!!");
        // 结束上拉加载
        [self.tableView footerEndRefreshing];
        
        //把接收到的数组对象变成model
        NSDictionary *jsonDic =  [NSJSONSerialization JSONObjectWithData:responseData options:NSJSONReadingMutableContainers error:NULL];
        NSString *value = jsonDic[@"data"];
        NSArray *dicArray = [NSArray array];
        if (value) {
            NSData *valueData = [value dataUsingEncoding:NSUTF8StringEncoding];
            dicArray = [CGInfo objectArrayWithJSONData:valueData];
        }
        
        if ([self.model isEqualToString:@"0"]) {
            //把数组中的元素添加进去
            [self.releaseProducts addObjectsFromArray:dicArray];
            
        }else{
            //把数组中的元素添加进去
            [self.overdueProducts addObjectsFromArray:dicArray];
        }
        
        // 刷新表格
        [self.tableView reloadData];
        
    } failure:^(NSError *error) {
        
        NSLog(@"过期信息加载请求失败:%@",error);
    }];
    
}

#pragma mark 分类按钮点击事件
-(void)collectionButtonClick:(UIButton *)sender{
    
    if (sender == self.releaseButton) {
        NSLog(@"选择发布的信息");
        self.releaseTableView.hidden = NO;
        self.overdueTableView.hidden = YES;
        self.releaseButton.selected = YES;
        self.overdueButton.selected = NO;
        self.firstLine.hidden = NO;
        self.secondLine.hidden = YES;
        self.model = @"0";
        
    }else{
        NSLog(@"选择已过期信息");
        self.releaseTableView.hidden = YES;
        self.overdueTableView.hidden = NO;
        self.releaseButton.selected = NO;
        self.overdueButton.selected = YES;
        self.firstLine.hidden = YES;
        self.secondLine.hidden = NO;
        self.model = @"1";
    }
    
    [self fetchCollectionProducts];
    
}

#pragma mark 右侧编辑按钮点击事件
-(void)enterEditing:(UIBarButtonItem *)sender{
    
    CGRect aRect = CGRectMake(0,51,SCREEN_WIDTH,SCREEN_HEIGHT-64-51);
    CGRect bRect = CGRectMake(0, SCREEN_HEIGHT-64, SCREEN_WIDTH, 49);
    CGRect tableFrame = CGRectMake(0,51,SCREEN_WIDTH,SCREEN_HEIGHT-64-51-49);
    CGRect bottomFrame = CGRectMake(0,SCREEN_HEIGHT-64-49,SCREEN_WIDTH,49);
    
    //[self.tableView setEditing:!self.tableView.editing animated:YES];
    if ([sender.title isEqualToString:@"编辑"]) {
        sender.title = @"取消";
        [UIView animateWithDuration:0.2 animations:^{
            self.releaseTableView.frame = tableFrame;
            self.overdueTableView.frame = tableFrame;
            self.bottomView.frame = bottomFrame;
        }];
        
        //每次只能编辑发布中或已过期中的一个类目
        if ([self.model isEqualToString:@"0"]) {
            self.releaseButton.enabled = YES;
            self.overdueButton.enabled = NO;
        }else{
            self.releaseButton.enabled = NO;
            self.overdueButton.enabled = YES;
        }
        
    }else{
        sender.title = @"编辑";
        [UIView animateWithDuration:0.2 animations:^{
            self.releaseTableView.frame = aRect;
            self.overdueTableView.frame = aRect;
            self.bottomView.frame = bRect;
        }];
        
        //把所有已发布中的信息前的对勾去除
        [self removeCircleWithTableView:self.releaseTableView andArray:self.releaseProducts];
        [self removeCircleWithTableView:self.overdueTableView andArray:self.overdueProducts];
        
        //还原按钮的功能
        self.releaseButton.enabled = YES;
        self.overdueButton.enabled = YES;
    }
    
    self.selectAllButton.selected = NO;
    
    [self.overdueTableView reloadData];
    [self.releaseTableView reloadData];
    
}

#pragma mark 去除单元格前面的对勾
-(void)removeCircleWithTableView:(UITableView *)tableView andArray:(NSMutableArray *)array{
    
    NSInteger rows =  array.count;
    for (int row = 0; row < rows; row++) {
        NSIndexPath *indexPath = [NSIndexPath indexPathForRow:row inSection:0];
        CGInfo *product = array[row];
        if (!product.type) {
            CGCollectionCompanyCell *cell = [tableView cellForRowAtIndexPath:indexPath];
            cell.circleImage.image = [UIImage imageNamed:@"mine_not_check_icon"];
            cell.isCircle = NO;
            
        }else{
            CGCollectionSchoolCell *cell = [tableView cellForRowAtIndexPath:indexPath];
            cell.circleImage.image = [UIImage imageNamed:@"mine_not_check_icon"];
            cell.isCircle = NO;
        }
    }
}

#pragma mark 删除按钮点击事件
-(void)deleteButtonClick{
    
    if ([self.navigationItem.rightBarButtonItem.title isEqualToString:@"取消"] && [self.model isEqualToString:@"0"]) {
        //获取将要删除信息的id(发布中)
        NSMutableArray *releaseArray = [self fetchSelectedCellWithTableView:self.releaseTableView andArray:self.releaseProducts];
        if (releaseArray.count == 0) {
            return;
        }
        
        //把选中的内容上传到服务器
        for (int i = 0 ; i < releaseArray.count; i ++) {
            
            NSString *info_id = releaseArray[i];
            NSLog(@"info_id:%@",info_id);
            NSMutableDictionary *parameters = [NSMutableDictionary dictionary];
            //用户ID
            parameters[@"id"] = [CGAccountDefault shareAccount].user_id;
            //企业招聘的ID
            parameters[@"collectionId"] = info_id;
            
            ZYHRequest *request = [ZYHRequest new];
            [request postAsyncEncryptRequest:@"/collection/delete.app" parameters:parameters success:^(id responseData) {
                
                NSString *result = [[NSString alloc]initWithData:responseData encoding:NSUTF8StringEncoding];
                
                if ([result isEqualToString:@"1"]) {   //返回：1成功 或 -1错误
                    NSLog(@"单元格上传成功");
                    
                }
                
            } failure:^(NSError *error) {
                
                NSLog(@"单元格上传失败");
                
            }];
            
            //信息删除完要更新表格
            [self fetchCollectionProducts];
            
        }
        
        
    }else if ([self.navigationItem.rightBarButtonItem.title isEqualToString:@"取消"] && [self.model isEqualToString:@"1"]) {
        
        //获取将要删除的信息的id(已过期)
        NSMutableArray *overdueArray = [self fetchSelectedCellWithTableView:self.overdueTableView andArray:self.overdueProducts];
        if (overdueArray.count == 0) {
            return;
        }
        
        //把选中的内容上传到服务器
        for (int i = 0 ; i < overdueArray.count; i ++) {
            
            NSString *info_id = overdueArray[i];
            NSLog(@"info_id:%@",info_id);
            NSMutableDictionary *parameters = [NSMutableDictionary dictionary];
            //用户ID
            parameters[@"id"] = [CGAccountDefault shareAccount].user_id;
            //企业招聘的ID
            parameters[@"collectionId"] = info_id;
            
            ZYHRequest *request = [ZYHRequest new];
            [request postAsyncEncryptRequest:@"/collection/delete.app" parameters:parameters success:^(id responseData) {
                
                NSString *result = [[NSString alloc]initWithData:responseData encoding:NSUTF8StringEncoding];
                
                if ([result isEqualToString:@"1"]) {   //返回：1成功 或 -1错误
                    NSLog(@"单元格上传成功");
                    
                }
                
            } failure:^(NSError *error) {
                
                NSLog(@"单元格上传失败");
            }];
            
            //信息删除完要更新表格
            [self fetchCollectionProducts];
        }
    }
}


#pragma mark 捕获将要删除的信息的id
-(NSMutableArray *)fetchSelectedCellWithTableView:(UITableView *)tableView andArray:(NSMutableArray *)array{
    
    NSInteger rows =  array.count;
    NSMutableArray *deleteArray = [NSMutableArray array];
    
    for (int row = 0; row < rows; row++) {
        NSIndexPath *indexPath = [NSIndexPath indexPathForRow:row inSection:0];
        CGInfo *product = array[row];
        
        if (!product.type) {  //企业信息
            
            CGCollectionCompanyCell *cell = [tableView cellForRowAtIndexPath:indexPath];
            if (cell.isCircle) {
                [deleteArray addObject:product.info_id];
            }
            
        }else{    //学校信息
            
            CGCollectionSchoolCell *cell = [tableView cellForRowAtIndexPath:indexPath];
            if (cell.isCircle) {
                [deleteArray addObject:product.info_id];
            }
        }
    }
    
    return deleteArray;
}



#pragma mark overide 表格的代理方法实现
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    
    if (tableView == self.releaseTableView) {
        
        return self.releaseProducts.count;
        
    }else{
        
        return self.overdueProducts.count;
    }
    
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    if ([self.navigationItem.rightBarButtonItem.title isEqualToString:@"编辑"]) {
        
        return [self normalDisplayWithTableView:tableView andIndexPath:indexPath];
        
    }else{
        
        return [self editDisplayWithTableView:tableView andIndexPath:indexPath];
        
    }
    
}

#pragma mark 正常显示TableView
-(UITableViewCell *)normalDisplayWithTableView:(UITableView *)tableView andIndexPath:(NSIndexPath *)indexPath {
    
    if (tableView == self.releaseTableView) {
        
        CGInfo *releasePro = self.releaseProducts[indexPath.row];
        if (!releasePro.type) {
            
            CGCollectionCompanyCell *cell = [self.releaseTableView dequeueReusableCellWithIdentifier:@"company"];
            if (cell == nil) {
                cell = [[CGCollectionCompanyCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"company"];
            }
            cell.circleImage.hidden = YES;
            //模型转视图模型
            CGCollectionCompanyFrame *companyFrame = [CGCollectionCompanyFrame new];
            companyFrame.product = releasePro;
            cell.productFrame = companyFrame;
            return cell;
            
        }else{
            
            CGCollectionSchoolCell *cell = [self.releaseTableView dequeueReusableCellWithIdentifier:@"school"];
            if (cell == nil) {
                cell = [[CGCollectionSchoolCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"school"];
            }
            cell.circleImage.hidden = YES;
            //模型转视图模型
            CGCollectionSchoolFrame *schoolFrame = [CGCollectionSchoolFrame new];
            schoolFrame.product = releasePro;
            cell.productFrame = schoolFrame;
            return cell;
        }
        
        
    }else{
        
        CGInfo *overduePro = self.overdueProducts[indexPath.row];
        if (!overduePro.type) {
            
            CGCollectionCompanyCell *cell = [self.overdueTableView dequeueReusableCellWithIdentifier:@"company"];
            if (cell == nil) {
                cell = [[CGCollectionCompanyCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"company"];
            }
            cell.circleImage.hidden = YES;
            //模型转视图模型
            CGCollectionCompanyFrame *companyFrame = [CGCollectionCompanyFrame new];
            companyFrame.product = overduePro;
            cell.productFrame = companyFrame;
            return cell;
            
        }else{
            
            CGCollectionSchoolCell *cell = [self.overdueTableView dequeueReusableCellWithIdentifier:@"school"];
            if (cell == nil) {
                cell = [[CGCollectionSchoolCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"school"];
            }
            cell.circleImage.hidden = YES;
            //模型转视图模型
            CGCollectionSchoolFrame *schoolFrame = [CGCollectionSchoolFrame new];
            schoolFrame.product = overduePro;
            cell.productFrame = schoolFrame;
            return cell;
        }
    }
    
}

#pragma mark 编辑时显示TableView
-(UITableViewCell *)editDisplayWithTableView:(UITableView *)tableView andIndexPath:(NSIndexPath *)indexPath {
    
    if (tableView == self.releaseTableView) {
        
        CGInfo *releasePro = self.releaseProducts[indexPath.row];
        if (!releasePro.type) {
            
            CGCollectionCompanyCell *cell = [self.releaseTableView dequeueReusableCellWithIdentifier:@"company"];
            if (cell == nil) {
                cell = [[CGCollectionCompanyCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"company"];
            }
            cell.circleImage.hidden = NO;
            //模型转视图模型
            CGCollectionCompanyFrame *companyFrame = [CGCollectionCompanyFrame new];
            companyFrame.product = releasePro;
            cell.productFrame = companyFrame;
            return cell;
            
        }else{
            
            CGCollectionSchoolCell *cell = [self.releaseTableView dequeueReusableCellWithIdentifier:@"school"];
            if (cell == nil) {
                cell = [[CGCollectionSchoolCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"school"];
            }
            cell.circleImage.hidden = NO;
            //模型转视图模型
            CGCollectionSchoolFrame *schoolFrame = [CGCollectionSchoolFrame new];
            schoolFrame.product = releasePro;
            cell.productFrame = schoolFrame;
            return cell;
        }
        
        
    }else{
        
        CGInfo *overduePro = self.overdueProducts[indexPath.row];
        if (!overduePro.type) {
            
            CGCollectionCompanyCell *cell = [self.overdueTableView dequeueReusableCellWithIdentifier:@"company"];
            if (cell == nil) {
                cell = [[CGCollectionCompanyCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"company"];
            }
            cell.circleImage.hidden = NO;
            //模型转视图模型
            CGCollectionCompanyFrame *companyFrame = [CGCollectionCompanyFrame new];
            companyFrame.product = overduePro;
            cell.productFrame = companyFrame;
            return cell;
            
        }else{
            
            CGCollectionSchoolCell *cell = [self.overdueTableView dequeueReusableCellWithIdentifier:@"school"];
            if (cell == nil) {
                cell = [[CGCollectionSchoolCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"school"];
            }
            cell.circleImage.hidden = NO;
            //模型转视图模型
            CGCollectionSchoolFrame *schoolFrame = [CGCollectionSchoolFrame new];
            schoolFrame.product = overduePro;
            cell.productFrame = schoolFrame;
            return cell;
        }
    }
    
}

//设置单元格高度
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{

    if (tableView == self.releaseTableView) {
        
        CGInfo *releasePro = self.releaseProducts[indexPath.row];
        if (!releasePro.type) {
            //模型转视图模型
            CGCollectionCompanyFrame *companyFrame = [CGCollectionCompanyFrame new];
            companyFrame.product = releasePro;
            return companyFrame.cellHeight;
            
        }else{
            //模型转视图模型
            CGCollectionSchoolFrame *schoolFrame = [CGCollectionSchoolFrame new];
            schoolFrame.product = releasePro;
            return schoolFrame.cellHeight;
        }
        
        
    }else{
        
        CGInfo *overduePro = self.overdueProducts[indexPath.row];
        if (!overduePro.type) {
            //模型转视图模型
            CGCollectionCompanyFrame *companyFrame = [CGCollectionCompanyFrame new];
            companyFrame.product = overduePro;
            return companyFrame.cellHeight;
            
        }else{
            //模型转视图模型
            CGCollectionSchoolFrame *schoolFrame = [CGCollectionSchoolFrame new];
            schoolFrame.product = overduePro;
            return schoolFrame.cellHeight;
        }
    }

}


#pragma mark 发送请求获取企业招聘详情
-(void)fetchCompanyProWithProID:(NSString *)product_id{
    
    //2.发送登录请求
    NSMutableDictionary *parameters = [NSMutableDictionary dictionary];
    parameters[@"userId"] = [CGAccountDefault shareAccount].user_id;
    parameters[@"id"] = product_id;
    ZYHRequest *request = [ZYHRequest new];
    [request postAsyncEncryptRequest:@"/company/showInfo.app" parameters:parameters success:^(id responseData) {
        
        NSString *result = [[NSString alloc]initWithData:responseData encoding:NSUTF8StringEncoding];
        //去除前面字符串
        if ([result rangeOfString:@"true"].location != NSNotFound) {
            NSMutableString *mstr = [NSMutableString stringWithString:result];
            NSRange range = NSMakeRange(0, 7);
            [mstr deleteCharactersInRange:range];
            result = mstr;
            //json转对象
            NSData *valueData = [result dataUsingEncoding:NSUTF8StringEncoding];
            self.company = [CGInfoCompany objectWithJSONData:valueData];
            //跳转
            CGCompanyJobDetailViewController *companyDetailVC = [CGCompanyJobDetailViewController new];
            companyDetailVC.myCompanyInfo = self.company;
            [self.navigationController pushViewController:companyDetailVC animated:YES];
            
        }else{
            
            [Tools alertWithTitle:@"温馨提示" andMessage:@"网络超时,请稍后再试"];
            
        }
        
    } failure:^(NSError *error) {
        
        [Tools alertWithTitle:@"温馨提示" andMessage:@"网络超时,请稍后再试"];
        
    }];
}


#pragma mark 发送请求获取学校招聘详情
-(void)fetchSchoolProWithProID:(NSString *)product_id{
    
    //2.发送登录请求
    NSMutableDictionary *parameters = [NSMutableDictionary dictionary];
    parameters[@"userId"] = [CGAccountDefault shareAccount].user_id;
    parameters[@"id"] = product_id;
    ZYHRequest *request = [ZYHRequest new];
    [request postAsyncEncryptRequest:@"/school/showInfo.app" parameters:parameters success:^(id responseData) {
        
        NSString *result = [[NSString alloc]initWithData:responseData encoding:NSUTF8StringEncoding];
        //去除前面字符串
        if ([result rangeOfString:@"true"].location != NSNotFound) {
            NSMutableString *mstr = [NSMutableString stringWithString:result];
            NSRange range = NSMakeRange(0, 7);
            [mstr deleteCharactersInRange:range];
            result = mstr;
            //json转对象
            NSData *valueData = [result dataUsingEncoding:NSUTF8StringEncoding];
            self.school = [CGInfoSchool objectWithJSONData:valueData];
            //跳转
            CGSchoolJobDetailViewController *schoolDetailVC = [CGSchoolJobDetailViewController new];
            schoolDetailVC.mySchoolInfo = self.school;
            [self.navigationController pushViewController:schoolDetailVC animated:YES];
            
        }else{
            
            [Tools alertWithTitle:@"温馨提示" andMessage:@"网络超时,请稍后再试"];
            
        }
        
    } failure:^(NSError *error) {
        
        [Tools alertWithTitle:@"温馨提示" andMessage:@"网络超时,请稍后再试"];
        
    }];
}






-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    
    self.company = nil;
    self.school = nil;
    if ([self.navigationItem.rightBarButtonItem.title isEqualToString:@"编辑"]) {
        
        if (tableView == self.releaseTableView) {
            
            CGInfo *releasePro = self.releaseProducts[indexPath.row];
            NSString *release_id = releasePro.info_id;
            
            if (!releasePro.type) {  //企业信息
                
                [self fetchCompanyProWithProID:release_id];
                
            }else{     //学校信息
                
                [self fetchSchoolProWithProID:release_id];
                
            }
            
        }else{
            
            CGInfo *overduePro = self.overdueProducts[indexPath.row];
            NSString *overdue_id = overduePro.info_id;
            
            if (!overduePro.type) {   //企业信息
                
                [self fetchCompanyProWithProID:overdue_id];
                
                
            }else{   //学校信息
                
                [self fetchSchoolProWithProID:overdue_id];
                
            }
        }
        
    }
    
    
    //选中时在单元格前面打勾
    if ([self.navigationItem.rightBarButtonItem.title isEqualToString:@"取消"]) {
        
        if (tableView == self.releaseTableView) {
            
            CGInfo *releasePro = self.releaseProducts[indexPath.row];
            if (!releasePro.type) {
                CGCollectionCompanyCell *companyCell = [self.releaseTableView cellForRowAtIndexPath:indexPath];
                if (!companyCell.isCircle) {
                    companyCell.circleImage.image = [UIImage imageNamed:@"mine_checked_icon"];
                    companyCell.isCircle = YES;
                }else{
                    companyCell.circleImage.image = [UIImage imageNamed:@"mine_not_check_icon"];
                    companyCell.isCircle = NO;
                }
                
            }else{
                
                CGCollectionSchoolCell *schoolCell = [tableView cellForRowAtIndexPath:indexPath];
                if (!schoolCell.isCircle) {
                    schoolCell.circleImage.image = [UIImage imageNamed:@"mine_checked_icon"];
                    schoolCell.isCircle = YES;
                }else{
                    schoolCell.circleImage.image = [UIImage imageNamed:@"mine_not_check_icon"];
                    schoolCell.isCircle = NO;
                }
                
            }
            
        }else{
            
            CGInfo *overduePro = self.overdueProducts[indexPath.row];
            if (!overduePro.type) {
                
                CGCollectionCompanyCell *companyCell = [self.overdueTableView cellForRowAtIndexPath:indexPath];
                if (!companyCell.isCircle) {
                    companyCell.circleImage.image = [UIImage imageNamed:@"mine_checked_icon"];
                    companyCell.isCircle = YES;
                }else{
                    companyCell.circleImage.image = [UIImage imageNamed:@"mine_not_check_icon"];
                    companyCell.isCircle = NO;
                }
                
            }else{
                
                CGCollectionSchoolCell *schoolCell = [self.overdueTableView cellForRowAtIndexPath:indexPath];
                if (!schoolCell.isCircle) {
                    schoolCell.circleImage.image = [UIImage imageNamed:@"mine_checked_icon"];
                    schoolCell.isCircle = YES;
                }else{
                    schoolCell.circleImage.image = [UIImage imageNamed:@"mine_not_check_icon"];
                    schoolCell.isCircle = NO;
                }
                
            }
        }
        
    }
    
}

#pragma mark 选中全部单元格
-(void)selectAllCellWithTableView:(UITableView *)tableView andArray:(NSMutableArray *)array{
    
    NSInteger rows =  array.count;
    for (int row = 0; row < rows; row++) {
        NSIndexPath *indexPath = [NSIndexPath indexPathForRow:row inSection:0];
        CGInfo *product = array[indexPath.row];
        if (product.type) {   //企业
            CGCollectionCompanyCell *cell = [tableView cellForRowAtIndexPath:indexPath];
            cell.circleImage.image = [UIImage imageNamed:@"mine_checked_icon"];
            cell.isCircle = YES;
        }else{   //学校
            CGCollectionSchoolCell *cell = [tableView cellForRowAtIndexPath:indexPath];
            cell.circleImage.image = [UIImage imageNamed:@"mine_checked_icon"];
            cell.isCircle = YES;
        }
    }
}

#pragma mark 去除单元格前面的对勾
-(void)cancleCheckedOfCellWithTableView:(UITableView *)tableView andArray:(NSMutableArray *)array{
    
    NSInteger rows =  array.count;
    for (int row = 0; row < rows; row++) {
        NSIndexPath *indexPath = [NSIndexPath indexPathForRow:row inSection:0];
        CGInfo *product = array[indexPath.row];
        if (product.type) {   //企业
            CGCollectionCompanyCell *cell = [tableView cellForRowAtIndexPath:indexPath];
            cell.circleImage.image = [UIImage imageNamed:@"mine_not_check_icon"];
            cell.isCircle = NO;
        }else{   //学校
            CGCollectionSchoolCell *cell = [tableView cellForRowAtIndexPath:indexPath];
            cell.circleImage.image = [UIImage imageNamed:@"mine_not_check_icon"];
            cell.isCircle = NO;
        }
    }
}



#pragma mark 全选按钮点击事件
-(void)selectAllButtonClick:(UIButton *)sender{
    
    sender.selected = !sender.selected;
    
    if ([self.navigationItem.rightBarButtonItem.title isEqualToString:@"取消"] && [self.model isEqualToString:@"0"]) {
        if (self.releaseProducts.count == 0) {
            sender.selected = NO;
            return;
        }
        if (sender.selected) {
            [self selectAllCellWithTableView:self.releaseTableView andArray:self.releaseProducts];
        }else{
            [self cancleCheckedOfCellWithTableView:self.releaseTableView andArray:self.releaseProducts];
        }
        
        
    }else if ([self.navigationItem.rightBarButtonItem.title isEqualToString:@"取消"] && [self.model isEqualToString:@"1"]) {
        if (self.overdueProducts.count == 0) {
            sender.selected = NO;
            return;
        }
        if (sender.selected) {
            [self selectAllCellWithTableView:self.overdueTableView andArray:self.overdueProducts];
        }else{
            [self cancleCheckedOfCellWithTableView:self.overdueTableView andArray:self.overdueProducts];
        }
        
    }
    
}


//使cell的线条充满
- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath

{
    
    if ([cell respondsToSelector:@selector(setSeparatorInset:)]) {
        
        [cell setSeparatorInset:UIEdgeInsetsZero];
        
    }
    
    if ([cell respondsToSelector:@selector(setLayoutMargins:)]) {
        
        [cell setLayoutMargins:UIEdgeInsetsZero];
        
    }
}


@end
